home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / editors / mjovesrc.zoo / screen.c < prev    next >
C/C++ Source or Header  |  1992-04-04  |  23KB  |  1,083 lines

  1. /***************************************************************************
  2.  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
  3.  * is provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is    *
  5.  * included in all the files.                                              *
  6.  ***************************************************************************/
  7.  
  8. #include "jove.h"
  9. #include "fp.h"
  10. #include "ctype.h"
  11. #include "termcap.h"
  12. #include "disp.h"
  13. #include <signal.h>
  14.  
  15. int    AbortCnt,
  16.     tabstop = 8;
  17. bool
  18.     CanScroll = NO;
  19.  
  20. #ifdef    TERMCAP
  21. private void
  22.     (*TTins_line) proto((int, int, int)),
  23.     (*TTdel_line) proto((int, int, int));
  24. #endif    /* TERMCAP */
  25.  
  26. struct scrimage
  27.     *DesiredScreen = NULL,
  28.     *PhysScreen = NULL;
  29.  
  30. struct screenline    *Screen = NULL,    /* the screen (a bunch of screenline) */
  31.             *Curline = NULL;    /* current line */
  32.  
  33. private struct screenline   *Savelines = NULL;    /* another bunch (LI of them) */
  34.  
  35.  
  36. private char    *cursor;            /* offset into current Line */
  37.  
  38. char    *cursend;
  39.  
  40. int    CapCol,
  41.     CapLine,
  42.  
  43.     i_line,
  44.     i_col;
  45.  
  46. #ifdef    IBMPC
  47. extern unsigned char    CHPL;
  48. extern void        near normfun(),
  49.             near scr_win(),
  50.             near clr_page(),
  51.             near clr_eoln();
  52.  
  53. #endif
  54.  
  55. void
  56. make_scr()
  57. {
  58.     register int    i;
  59.     register struct screenline    *ns;
  60.     register char    *nsp;
  61.  
  62.     /* In case we are RESHAPING the window! */
  63.     if (DesiredScreen != NULL)
  64.         free((UnivPtr) DesiredScreen);
  65.     if (PhysScreen != NULL)
  66.         free((UnivPtr) PhysScreen);
  67.     if (Savelines != NULL)
  68.         free((UnivPtr) Savelines);
  69.     if (Screen != NULL) {
  70.         free((UnivPtr) Screen->s_line);    /* free all the screen data */
  71.         free((UnivPtr) Screen);
  72.     }
  73.  
  74.     DesiredScreen = (struct scrimage *) malloc((unsigned) LI * sizeof (struct scrimage));
  75.     PhysScreen = (struct scrimage *) malloc((unsigned) LI * sizeof (struct scrimage));
  76.  
  77.     Savelines = (struct screenline *)
  78.             malloc((unsigned) LI * sizeof(struct screenline));
  79.     ns = Screen = (struct screenline *)
  80.             malloc((unsigned) LI * sizeof(struct screenline));
  81.  
  82.     nsp = (char *) malloc((unsigned)CO * LI);
  83.  
  84.     if (DesiredScreen == NULL
  85.     || PhysScreen == NULL
  86.     || Savelines == NULL
  87.     || ns == NULL
  88.     || nsp == NULL)
  89.     {
  90.         writef("\n\rCannot malloc screen!\n");
  91.         finish(SIGHUP);
  92.     }
  93.  
  94.     for (i = 0; i < LI; i++) {
  95.         ns->s_line = nsp;
  96.         nsp += CO;
  97.         ns->s_length = nsp - 1;        /* End of Line */
  98.         ns += 1;
  99.     }
  100.     cl_scr(0);
  101. }
  102.  
  103. void
  104. clrline(cp1, cp2)
  105. register char    *cp1,
  106.         *cp2;
  107. {
  108.     while (cp1 <= cp2)
  109.         *cp1++ = ' ';
  110. }
  111.  
  112.  
  113. /* Output one character (if necessary) at the current position */
  114.  
  115. #ifdef    MAC
  116.  
  117. /* Character output to bit-mapped screen is very expensive. It makes
  118.    much more sense to write the entire line at once. So, we print all
  119.    the characters, whether already there or not, once the line is
  120.    complete.  */
  121.  
  122. private char sput_buf[256];
  123. private int sput_len = 0;
  124.  
  125. private void
  126. sput_start()
  127. {
  128. /*    if (i_line != CapLine || i_col != CapCol) */
  129.         NPlacur(i_line, i_col);
  130.     sput_len = 0;
  131. }
  132.  
  133. private void
  134. sput_end()
  135. {
  136.     sput_buf[0] = (unsigned char) sput_len;
  137.     writechr(sput_buf);
  138.     sput_len = 0;
  139. }
  140.  
  141. private void
  142. sputc(c)
  143. register int c;
  144. {
  145.     if (sput_len < sizeof(sput_buf)) {
  146.         *cursor++ = c;
  147.         sput_buf[++sput_len] = (c == '0')? 0xAF /* slashed zero */ : c;
  148.         CapCol++;
  149.         i_col++;
  150.     }
  151. }
  152.  
  153. #else    /* !MAC */
  154. #ifdef    IBMPC
  155.  
  156. private bool force = NO;
  157.  
  158. private void
  159. sputc(c)
  160. register int    c;
  161. {
  162.     if (force || (*cursor != c)) {
  163.         if (i_line != CapLine || i_col != CapCol)
  164.             Placur(i_line, i_col);
  165.         *cursor++ = c;
  166.         normfun((char) c);
  167.         AbortCnt -= 1;
  168.         CapCol += 1;
  169.     } else {
  170.         cursor += 1;
  171.     }
  172.     i_col += 1;
  173. }
  174.  
  175. #else    /* !IBMPC */
  176.  
  177. #  define sputc(c)    { \
  178.     if (*cursor != (char) (c)) { \
  179.         do_sputc(c); \
  180.     } else { \
  181.         cursor++; \
  182.         i_col++; \
  183.     } \
  184. }
  185.  
  186. private void
  187. do_sputc(c)
  188. register int    c;
  189. {
  190.     if (*cursor != c) {
  191. # ifdef    ID_CHAR
  192.         if (IN_INSmode)
  193.             INSmode(OFF);
  194. # endif
  195.         if (i_line != CapLine || i_col != CapCol)
  196.             Placur(i_line, i_col);
  197.         if (UL && (c & CHARMASK) == '_' && (*cursor & CHARMASK) != ' ')
  198.             putstr(" \b");        /* Erase so '_' looks right. */
  199.         *cursor++ = c;
  200.         jputchar(c & CHARMASK);
  201.         AbortCnt -= 1;
  202.         CapCol += 1;
  203.     } else {
  204.         cursor += 1;
  205.     }
  206.     i_col += 1;
  207. }
  208.  
  209. #endif    /* !IBMPC */
  210. #endif    /* !MAC */
  211.  
  212. void
  213. cl_eol()
  214. {
  215.     if (cursor > cursend)
  216.         return;
  217.  
  218.     if (cursor < Curline->s_length) {
  219. #ifdef    TERMCAP
  220.         if (CE) {
  221.             Placur(i_line, i_col);
  222.             putpad(CE, 1);
  223.             clrline(cursor, Curline->s_length);
  224.         } else {
  225.             /* Ugh.  The slow way for dumb terminals. */
  226.             register char *savecp = cursor;
  227.  
  228.             while (cursor <= Curline->s_length)
  229.                 sputc(' ');
  230.             cursor = savecp;
  231.         }
  232. #else    /* !TERMCAP */
  233.         Placur(i_line, i_col);
  234.         clr_eoln();
  235.         clrline(cursor, Curline->s_length);
  236. #endif    /* !TERMCAP */
  237.         Curline->s_length = cursor;
  238.     }
  239. }
  240.  
  241. void
  242. cl_scr(doit)
  243. bool doit;
  244. {
  245.     register int    i;
  246.     register struct screenline    *sp = Screen;
  247.  
  248.     for (i = 0; i < LI; i++, sp++) {
  249.         clrline(sp->s_line, sp->s_length);
  250.         sp->s_length = sp->s_line;
  251.         PhysScreen[i].s_id = 0;
  252.     }
  253.     if (doit) {
  254. #ifdef    TERMCAP
  255.         putpad(CL, LI);
  256. #else    /* !TERMCAP */
  257.         clr_page();
  258. #endif    /* !TERMCAP */
  259.         CapCol = CapLine = 0;
  260.         UpdMesg = YES;
  261.     }
  262. }
  263.  
  264. /* Write `line' at the current position of `cursor'.  Stop when we
  265.    reach the end of the screen.  Aborts if there is a character
  266.    waiting.  */
  267.  
  268.  
  269. bool
  270. swrite(line, inversep, abortable)
  271. register char    *line;
  272. bool    inversep;
  273. bool    abortable;
  274. {
  275.     register int    n = cursend - cursor;
  276.     bool    aborted = NO;
  277.  
  278.     if (n > 0) {
  279.  
  280.         register int    c;
  281.         int    col = i_col;
  282. #ifdef    MAC
  283. #        define    spit(c)    sputc(c)
  284. #else    /* !MAC */
  285. #ifdef    IBMPC
  286. #        define    spit(c)    sputc(c)
  287. #else    /* !IBMPC */
  288.         int    or_byte = inversep ? 0200 : 0;
  289. #        define    spit(c)    { int temp = (c) | or_byte; sputc(temp); }
  290. #endif    /* !IBMPC */
  291. #endif    /* !MAC */
  292.  
  293. #ifdef    MAC
  294.         sput_start();    /* Okay, because no interruption possible */
  295. #endif    /* MAC */
  296. #ifdef    IBMPC
  297.         force = inversep;  /* to force a redraw of the modeline */
  298. #endif
  299.         while ((c = *line++) != '\0') {
  300. #            define  spot(c) { if (--n <= 0) break; spit(c); col += 1; }
  301.  
  302.             if (abortable && AbortCnt < 0) {
  303.                 AbortCnt = BufSize;
  304.                 if ((InputPending = charp()) != NO) {
  305.                     aborted = YES;
  306.                     break;
  307.                 }
  308.             }
  309.             if (c == '\t') {
  310.                 int    nchars;
  311.  
  312.                 nchars = (tabstop - (col % tabstop));
  313.                 while (--nchars > 0)
  314.                     spot(' ');
  315.                 c = ' ';
  316.             } else if (jiscntrl(c)) {
  317.                 spot('^');
  318.                 c = (c == '\177') ? '?' : c + '@';
  319. #ifdef    TERMCAP
  320.             } else if (Hazeltine && c == '~') {
  321.                 c = '`';
  322. #endif
  323. #ifdef    IBMPC
  324.             } else if (c == 255) {
  325.                 c = 1;
  326.             } else if (c == ' ' && inversep) {
  327.                 c = 255;
  328. #endif    /* IBMPC */
  329.             }
  330.             spot(c);
  331. #            undef    spot
  332.         }
  333.         if (n <= 0)
  334.             spit(((*line=='\0') && (c!='\t') && !jiscntrl(c))? c : '!');
  335.         if (cursor > Curline->s_length)
  336.             Curline->s_length = cursor;
  337. #ifdef    MAC
  338.         sput_end();
  339. #endif    /* MAC */
  340. #ifdef    IBMPC
  341.         force = NO;
  342. #endif
  343. #        undef    spit
  344.     }
  345.     return !aborted;
  346. }
  347.  
  348. /* This is for writing a buffer line to the screen.  This is to
  349.    minimize the amount of copying from one buffer to another buffer.
  350.    This gets the info directly from the disk buffers. */
  351.  
  352.  
  353. bool
  354. BufSwrite(linenum)
  355. int linenum;
  356. {
  357.     register int    n = cursend - cursor,
  358.             col = 0,
  359.             c = -1;
  360.     register char    *bp;
  361.     int    StartCol = DesiredScreen[linenum].s_offset,
  362.         visspace = DesiredScreen[linenum].s_window->w_flags & W_VISSPACE;
  363.     bool    aborted = NO;
  364.  
  365.     bp = lcontents(DesiredScreen[linenum].s_lp);
  366.     if (*bp) {
  367.         for (;;) {
  368.             if (col >= StartCol) {
  369.                 DesiredScreen[linenum].s_offset = col;
  370.                 break;
  371.             }
  372.  
  373.             c = *bp++ & CHARMASK;
  374.             if (c == '\0')
  375.                 break;
  376.             if (c == '\t')
  377.                 col += (tabstop - (col % tabstop));
  378.             else if (jiscntrl(c))
  379.                 col += 2;
  380.             else
  381.                 col += 1;
  382.         }
  383.     }
  384. #ifdef    MAC
  385.     sput_start();    /* Okay because we can't be interrupted */
  386. #endif
  387.     if (c != '\0') {
  388.         while ((c = *bp++) != '\0') {
  389. #            define spot(c)  { if (--n <= 0) break; sputc(c); col += 1; }
  390.  
  391.             if (AbortCnt < 0) {
  392.                 AbortCnt = BufSize;
  393.                 if ((Input